package com.situ.staff.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.staff.bean.Dept;
import com.situ.staff.dao.DeptDao;
import com.situ.staff.service.DeptService;

@Service
public class DeptServiceImpl implements DeptService {

	@Autowired
	private DeptDao deptDao;

	@Override
	public int add(Dept dept) throws Exception {
		// 数据验证
		if (dept.getName().length() < 3 || dept.getName().length() > 16) {
			throw new Exception("部门名称格式不正确");
		}
		if (dept.getRoom().length() < 2 || dept.getRoom().length() > 10) {
			throw new Exception("办公地点格式不正确");
		}
		if (dept.getPhone().length() > 11) {
			throw new Exception("手机格式不正确");
		}

		// 添加数据库
		return deptDao.insert(dept);
	}

	@Override
	public List getAll() {

		return deptDao.secectAll();
	}

	@Override
	public Dept getById(Integer id) {

		return deptDao.selectById(id);
	}

	@Override
	public int update(Dept dept) throws Exception {
		if (dept.getName().length() < 3 || dept.getName().length() > 16) {
			throw new Exception("部门名称格式不正确");
		}
		if (dept.getRoom().length() < 2 || dept.getRoom().length() > 10) {
			throw new Exception("办公地点格式不正确");
		}
		if (dept.getPhone().length() > 11) {
			throw new Exception("手机格式不正确");
		}

		return deptDao.update(dept);
	}

	@Override
	public int remove(Integer id) {

		return deptDao.delete(id);
	}

}
